/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: RetainServiceImp.java
 * Author:   linyao
 * Date:     2016-7-4 下午2:46:36
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.service.marketing;

import com.emm.yixun.common.model.dto.HaveAdvanceRefundDto;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.marketing.service.HaveAdvanceRefundServiceFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


@Service
public class HaveAdvanceRefundServiceImp implements HaveAdvanceRefundService {

	@Autowired
	private HaveAdvanceRefundServiceFacade haveAdvanceRefundServiceFacade;

	@Override
	public boolean addRefund(HaveAdvanceRefundDto entity, long id, BigDecimal counterFee, BigDecimal realAmount,
							 String needOrHave) {
		boolean flag = false;
		if (counterFee.compareTo(new BigDecimal("0")) >= 1) {
			// 当退款金额大于0时，增加一条保存手续费
			entity.setRealAmount(counterFee); // 手续费
			entity.setBusinessFlag("16"); // 退款手续费
			Response<HaveAdvanceRefundDto> response = haveAdvanceRefundServiceFacade.save(entity);
			if (!response.isSuccess()) {
				return flag;
			}
		}

		entity.setRealAmount(realAmount); // 实退金额
		if (entity.getReceivablesType().equals(BusinessTypeEnum.DEPOSIT.getNo())) {
			entity.setBusinessFlag("4"); // 退认筹
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.SUBSCRIBE.getNo())) {
			entity.setBusinessFlag("8"); // 退认购
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.SIGNED.getNo())) {
			entity.setBusinessFlag("14"); // 退签约
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.AREA.getNo())) {
			entity.setBusinessFlag("15"); // 补差款
		}
		Response<HaveAdvanceRefundDto> response = haveAdvanceRefundServiceFacade.save(entity);
		if (response.isSuccess()) {
			flag = true;
		}

		return flag;
	}

	@Override
	public Double getRealAmount(HaveAdvanceRefundDto entity) {
		return haveAdvanceRefundServiceFacade.haveAdvanceAmount(entity);
	}
}
